import java.io.Serializable;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;


public class Course implements Serializable {
	
	private String courseID;
	private String courseName;
	private String courseUnit;
	private String numOfHours;

	/**
	 * @param id
	 * @param name
	 * @param num
	 * @param unit
	 */
	public Course(String id, String name, String num, String unit) {
		
		this.courseID = id;
		this.courseName = name;
		this.courseUnit = unit;
		this.numOfHours = num;
		
	}

	public Course() {
		
	}
	

	public Course(String id) {
		this.courseID = id;
		Course C = DBgetCourse(id);
		this.courseName = C.courseName;
		this.courseUnit = C.courseUnit;
		this.numOfHours = C.numOfHours;
	}

	/**
	 * @return the courseID
	 */
	public String getCourseID() {
		return courseID;
	}

	/**
	 * @param courseID the courseID to set
	 */
	public void setCourseID(String courseID) {
		this.courseID = courseID;
	}

	/**
	 * @return the courseName
	 */
	public String getCourseName() {
		return courseName;
	}

	/**
	 * @param courseName the courseName to set
	 */
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	/**
	 * @return the courseUnit
	 */
	public String getCourseUnit() {
		return courseUnit;
	}

	/**
	 * @param courseUnit the courseUnit to set
	 */
	public void setCourseUnit(String courseUnit) {
		this.courseUnit = courseUnit;
	}

	/**
	 * @return the numOfHours
	 */
	public String getNumOfHours() {
		return numOfHours;
	}

	/**
	 * @param numOfHours the numOfHours to set
	 */
	public void setNumOfHours(String numOfHours) {
		this.numOfHours = numOfHours;
	}
	
	public Course DBgetCourse(String id)
	{
		SQL s = new SQL(id,97);
		s.accept();
		return (Course)s.getMsg();
	}
	/**
	 * 
	 * @return classes which have this course
	 */
	public LinkedList<ClassInCourse> DBclassInCourse()
	{
		SQL s = new SQL(this.courseID,47);
		s.accept();
		LinkedList<ClassInCourse> list1 = (LinkedList<ClassInCourse>)s.getMsg();
		Iterator<ClassInCourse> itr1 = list1.iterator();
		System.out.println("this is the course id: "+this.courseID);
		while (itr1.hasNext())
		{
			ClassInCourse C = itr1.next();
			System.out.println("this is this course id: "+C.getCourseID()+" and class id: "+C.getClassID() );
			if (!C.getCourseID().equals(courseID))
			 itr1.remove();
		}
		return list1;		
	}
	public LinkedList<StudentList> DBstudentList()
	{
		SQL s = new SQL(this.courseID,98);
		s.accept();
		LinkedList<StudentList> list2 = (LinkedList<StudentList>)s.getMsg();
		Iterator<StudentList> itr2 = list2.iterator();
		while (itr2.hasNext())
		{
			StudentList SL = itr2.next();
			if ((!SL.getlID().equals(this.courseID))||(SL.getType()!=3))
			 itr2.remove();
		}
		return list2;		
	} 
	public LinkedList<String> DBpreCourse()
	{
		SQL s = new SQL(this.courseID,96);
		s.accept();
		return (LinkedList<String>)s.getMsg();
	}
	public void DBSHOW()
	{
	
		System.out.println("Now Courses data should show");
		LinkedList<ClassInCourse> listCiC = DBclassInCourse();
		LinkedList<StudentList> listSL = DBstudentList();
		Iterator<ClassInCourse> itrCIC = listCiC.iterator();
		Iterator<StudentList> itrSL = listSL.iterator();
		LinkedList<String> listPC = DBpreCourse();
		Iterator<String> itrPC = listPC.iterator();
		
		System.out.println("this is listCIC size: "+listCiC.size());
		/////////////////////////Start of Print area////////////////////////////////
		
		String output = "";
		if (!itrCIC.hasNext()) output += "There are no classes for this course\n";
		else
		{
			output += "Here are the classess and teachers "+this.courseName+" has\n";
			while (itrCIC.hasNext())
			{
				ClassInCourse CIC = itrCIC.next();
				output += "Class: "+CIC.getClassID()+" Teacher: "+CIC.getTeacherID()+"\n";
			}
		}
		if (!itrSL.hasNext()) output += "There are no students in this course\n";
		else
		{
			output +="Here are the students in this course\n";
			while (itrSL.hasNext())
			{
				StudentList SL = itrSL.next();
				output += SL.getsID()+"  ";
			}
		}
		if (!itrPC.hasNext()) output +="\nThere are no Pre-Courses for this course\n";
		else
		{
			output += "\nHere are the pre-courses for this course\n";
			while (itrPC.hasNext())
			{
				output += itrPC.next()+"  ";
			}
		}
		
		JTextArea outputArea = new JTextArea( 11, 60 );
		outputArea.setText( output );

		JOptionPane.showMessageDialog( null, outputArea,"Course Data",JOptionPane.INFORMATION_MESSAGE );
		
		/////////////////////////End of Print area////////////////////////////////

		
		
	}
}

